// GateServer project main.go

package main

import (
	"../medusa"
	"flag"
	"fmt"
	gocfg "github.com/robfig/config"
	"runtime"
)

//1.pre-send 有问题 ，socket.go:269
//2.accept时，启动go{先从fd接收一次数据，保证fd没有问题再调用acc.startup， 有问题的话释放acc}
//3.listen增加预处理机制，也就是在没有分配acc时进行一些协议处理：比如排队 (Socket.startup增加第二个参数可以传入预接收的数据)
//4.global代码转到对应的gate和world中，
//5.独立心跳、haslog等接口，尽量让socket.go干净点
//6.accept线程数=max_conn/100  那么最大连接数其实是：max_conn+max_conn/100
//7.listen启动监控线程，当accept线程数量=0 向join chan发送数据 && SetStateStopped()，join chan每次run时重新make一个
//8.要考虑tasklet什么时候Close
//9.taskmonitor和gomonitor 管理这些对象
//import "sync"

func main() {

	bb := medusa.NewByteBuffer()
    bb.WriteInt16LE(11)
	bb.WriteString("中国人!")
    bb.WriteInt32LE(13)
    bb.WriteFloat32BE (3.1415926)
    bb.WriteFloat64LE (123.12345678)
	fmt.Printf("len:%v val:%v\n", bb.Len(), bb.ReadInt16LE())
    fmt.Printf("len:%v val:%v\n", bb.Len(), bb.ReadString())
    fmt.Printf("len:%v val:%v\n", bb.Len(), bb.ReadInt32LE())
    fmt.Printf("len:%v val:%v\n", bb.Len(), bb.ReadFloat32BE())
    fmt.Printf("len:%v val:%v\n", bb.Len(), bb.ReadFloat64LE())
	//bb.Clear()
	//bb.WriteInt16LE(11)
	//fmt.Printf("len:%v\n", bb.Len())

	//return
	/*
		ta := func() bool {
			fmt.Println("ta")
			return true
		}

		tb := func() bool {
			fmt.Println("tb")
			return true
		}
		if true &&ta() && tb() {
			fmt.Println("if")
		}
	    fmt.Println("if-end")
	*/
	/*
		     var aaa int = 0
			var once sync.Once
			var i int
			add := func() {
		        aaa = aaa + 1

			}
			for i = 0; i < 100; i++ {
				go func() {
					once.Do(add)
					fmt.Printf("aaa:=%v\n",aaa)
				}()
			}*/

	//return
	config_file := flag.String("conf", "gate.cfg",
		"gateserver starup config file")
	flag.Parse()

	cfg, _ := gocfg.ReadDefault(*config_file)
	maxprocs, _ := cfg.Int("runtime", "maxprocs")
	loglv, _ := cfg.Int("runtime", "loglv")
	//maxonline, _ := cfg.Int("runtime", "maxonline")
	runtime.GOMAXPROCS(maxprocs)

	world_listen_name, _ := cfg.String("world_listen", "name")
	world_listen_ip, _ := cfg.String("world_listen", "ip")
	world_listen_port, _ := cfg.Int("world_listen", "port")
	world_listen_rcvbuf, _ := cfg.Int("world_listen", "rcvbuf")
	world_listen_sndbuf, _ := cfg.Int("world_listen", "sndbuf")
	world_listen_sndchan, _ := cfg.Int("world_listen", "sndchan")
	world_listen_maxconn, _ := cfg.Int("world_listen", "maxconn")
	world_listen_maxidle, _ := cfg.Int("world_listen", "maxidle")

	gate_listen_name, _ := cfg.String("gate_listen", "name")
	gate_listen_ip, _ := cfg.String("gate_listen", "ip")
	gate_listen_port, _ := cfg.Int("gate_listen", "port")
	gate_listen_rcvbuf, _ := cfg.Int("gate_listen", "rcvbuf")
	gate_listen_sndbuf, _ := cfg.Int("gate_listen", "sndbuf")
	gate_listen_sndchan, _ := cfg.Int("gate_listen", "sndchan")
	gate_listen_maxconn, _ := cfg.Int("gate_listen", "maxconn")
	gate_listen_maxidle, _ := cfg.Int("gate_listen", "maxidle")

	fmt.Println("runtime\t\tvalue")
	fmt.Println("-----------------------------------------------------------------------")
	fmt.Printf("%v\t\t%v\n", "version", "1.0 beta")
	fmt.Printf("%v\t%v\n", "maxprocs", maxprocs)
	fmt.Printf("%v\t\t%v\n\n", "loglv", loglv)

	fmt.Println("info\tip\t\t\tport\tmaxconn\tmaxidle\trcvbuf\tsndbuf\tsndchan")
	fmt.Println("-----------------------------------------------------------------------")
	fmt.Printf("%v\t%v\t%v\t%v\t\t%v\t%vK\t%vK\t%v\n", world_listen_name, world_listen_ip,
		world_listen_port, world_listen_maxconn, world_listen_maxidle,
		world_listen_rcvbuf/1024, world_listen_sndbuf/1024, world_listen_sndchan)
	fmt.Printf("%v\t%v\t%v\t%v\t\t%v\t%vK\t%vK\t%v\n\n", gate_listen_name, gate_listen_ip,
		gate_listen_port, gate_listen_maxconn, gate_listen_maxidle,
		gate_listen_rcvbuf/1024, gate_listen_sndbuf/1024, gate_listen_sndchan)

	log := medusa.DefaultLog()
	log.SetLv(loglv)

	//evp := network.NewEventProcessor()
	world_service := NewWorldService(world_listen_name,
		world_listen_maxconn, world_listen_rcvbuf, world_listen_sndbuf, world_listen_sndchan, (int64)(world_listen_maxidle), log)
	gate_service := NewGateService(gate_listen_name,
		gate_listen_maxconn, gate_listen_rcvbuf, gate_listen_sndbuf, gate_listen_sndchan, (int64)(gate_listen_maxidle), log)

	NewGlobal(gate_service)

	err := world_service.Run("0.0.0.0", world_listen_port)
	if err != nil {
		log.Error("System", "Starup GateServer listener failed:%v", err)
		return
	}
	log.Success("System", "Starup World listener succeed!")

	err = gate_service.Run("0.0.0.0", gate_listen_port)
	if err != nil {
		log.Error("System", "Starup GateServer listener failed:%v", err)
		return
	}
	log.Success("System", "Starup GateServer listener succeed!")
	world_service.Wait()
	gate_service.Wait()
}
